Set與List概念上類似,最大的差異在於Set是無順序且key不能重覆。
可用指令
資料結構
在Set key中加入member.
127.0.0.1:6379> sadd books abc def
(integer) 2
取得Set key中所有的member.
127.0.0.1:6379> sadd books abc def
(integer) 2
127.0.0.1:6379> smembers books
1) "def"
2) "abc"
隨機取得Set key中的member數.
# 加入四個member到books key中
127.0.0.1:6379> sadd books abc def ghi jkl
(integer) 4
# 取得books key中所有的member
127.0.0.1:6379> smembers books
1) "ghi"
2) "def"
3) "abc"
4) "jkl"
# 隨機取得books key中的任兩個member
127.0.0.1:6379> srandmember books 2
1) "def"
2) "jkl"
# 隨機取得books key中的任兩個member
127.0.0.1:6379> srandmember books 2
1) "def"
2) "jkl"
# 隨機取得books key中的任兩個member (不同)
127.0.0.1:6379> srandmember books 2
1) "abc"
2) "def"
刪除Set key中的特定member.
127.0.0.1:6379> sadd books abc def ghi jkl
(integer) 4
# 移除books key中的member abc
127.0.0.1:6379> srem books abc
(integer) 1
# 移除books key中的member abc,不存在此member,所以失敗
127.0.0.1:6379> srem books abc
(integer) 0
127.0.0.1:6379> smembers books
1) "ghi"
2) "def"
3) "jkl"
刪除Set key中的隨機member數.
127.0.0.1:6379> sadd books abc def ghi jkl
(integer) 4
# 隨機刪除2個books中的member
127.0.0.1:6379> spop books 2
1) "jkl"
2) "ghi"
127.0.0.1:6379> smembers books
1) "def"
2) "abc"
# 隨機刪除1個books中的member
127.0.0.1:6379> spop books 1
1) "def"
127.0.0.1:6379> smembers books
1) "abc"
取得多個key中member的聯集.
127.0.0.1:6379> sadd books abc def
(integer) 2
127.0.0.1:6379> sadd books2 def ghi jkl
(integer) 3
127.0.0.1:6379> smembers books
1) "def"
2) "abc"
127.0.0.1:6379> smembers books2
1) "def"
2) "jkl"
3) "ghi"
127.0.0.1:6379> sinter books books2
1) "def"
取得多個key中member的交集.
127.0.0.1:6379> sadd books abc def
(integer) 2
127.0.0.1:6379> sadd books2 def ghi jkl
(integer) 3
127.0.0.1:6379> smembers books
1) "def"
2) "abc"
127.0.0.1:6379> smembers books2
1) "def"
2) "jkl"
3) "ghi"
127.0.0.1:6379> sunion books books2
1) "def"
2) "abc"
3) "ghi"
4) "jkl"
取得多個key中member的差集
127.0.0.1:6379> sadd books abc def
(integer) 2
127.0.0.1:6379> sadd books2 def ghi jkl
(integer) 3
127.0.0.1:6379> smembers books
1) "def"
2) "abc"
127.0.0.1:6379> smembers books2
1) "def"
2) "jkl"
3) "ghi"
# 注意前後key的擺放會影響差集結果
127.0.0.1:6379> sdiff books books2
1) "abc"
# 注意前後key的擺放會影響差集結果
127.0.0.1:6379> sdiff books2 books
1) "ghi"
2) "jkl"
判斷key中的member是否有符合指定的member.
127.0.0.1:6379> sadd books abc def
(integer) 2
127.0.0.1:6379> smembers books
1) "def"
2) "abc"
# 判斷member abc是否有在books key的member中 (有)
127.0.0.1:6379> sismember books abc
(integer) 1
# 判斷member ghi是否有在books key的member中 (沒有)
127.0.0.1:6379> sismember books ghi
(integer) 0
將某key中的指定member移至目的端key的member中.
127.0.0.1:6379> sadd books abc def
(integer) 2
127.0.0.1:6379> sadd books2 def ghi jkl
(integer) 3
127.0.0.1:6379> smembers books
1) "def"
2) "abc"
127.0.0.1:6379> smembers books2
1) "def"
2) "jkl"
3) "ghi"
# 從books2 key中移動jkl member到books key中
127.0.0.1:6379> smove books2 books jkl
(integer) 1
127.0.0.1:6379> smembers books
1) "def"
2) "abc"
3) "jkl"
127.0.0.1:6379> smembers books2
1) "def"
2) "ghi"
取得key中member的總數.
127.0.0.1:6379> sadd books abc def
(integer) 2
127.0.0.1:6379> sadd books2 def ghi jkl
(integer) 3
# books key中有2個member
127.0.0.1:6379> scard books
(integer) 2
# books key中有3個member
127.0.0.1:6379> scard books2
(integer) 3
# 此nobody key不存在,回傳0
127.0.0.1:6379> scard nobody
(integer) 0
掃描key中指定member 特定match pattern與次數.
# 取得1個存在books key中member為a開頭
127.0.0.1:6379> sscan books 0 match a* 1
1) "0"
2) 1) "abc"
# 取得1個存在books key中member為b開頭
127.0.0.1:6379> sscan books 0 match b* 1
1) "0"
2) (empty array)
取得多個key中member的聯集,並存入另外一個key中.
127.0.0.1:6379> sadd books abc def
(integer) 2
127.0.0.1:6379> sadd books2 def ghi jkl
(integer) 3
127.0.0.1:6379> smembers books
1) "def"
2) "abc"
127.0.0.1:6379> smembers books2
1) "def"
2) "jkl"
3) "ghi"
127.0.0.1:6379> sinterstore books books2
1) "def"
# 將books key與books2 key中的member聯集寫入到newbooks key中
127.0.0.1:6379> sinterstore newbooks books books2
(integer) 1
127.0.0.1:6379> smembers newbooks
1) "def"
取得多個key中member的交集,並存入另外一個key中.
127.0.0.1:6379> sadd books abc def
(integer) 2
127.0.0.1:6379> sadd books2 def ghi jkl
(integer) 3
127.0.0.1:6379> smembers books
1) "def"
2) "abc"
127.0.0.1:6379> smembers books2
1) "def"
2) "jkl"
3) "ghi"
# 將books key與books2 key中的member交集寫入到newbooks2 key中
127.0.0.1:6379> sunionstore newbooks2 books books2
(integer) 4
127.0.0.1:6379> smembers newbooks2
1) "def"
2) "abc"
3) "ghi"
4) "jkl"
取得多個key中member的差集,並存入另外一個key中.
127.0.0.1:6379> sadd books abc def
(integer) 2
127.0.0.1:6379> sadd books2 def ghi jkl
(integer) 3
127.0.0.1:6379> smembers books
1) "def"
2) "abc"
127.0.0.1:6379> smembers books2
1) "def"
2) "jkl"
3) "ghi"
# 將books key與books2 key的member差集寫入newbooks3 key中
127.0.0.1:6379> sdiffstore newbooks3 books books2
(integer) 1
127.0.0.1:6379> smembers newbooks3
1) "abc"
# 將books2 key與books key的member差集寫入newbooks4 key中
127.0.0.1:6379> sdiffstore newbooks4 books2 books
(integer) 2
127.0.0.1:6379> smembers newbooks4
1) "ghi"
2) "jkl"